package com.mago.tis.serviceImpl;

import java.util.List;

import org.apache.log4j.Logger;

import com.mago.tis.dao.ITraderDao;
import com.mago.tis.dao.ITransportDao;
import com.mago.tis.daoImpl.TraderDao;
import com.mago.tis.daoImpl.TransportDao;
import com.mago.tis.pojo.Tismbt;
import com.mago.tis.pojo.Tismct;
import com.mago.tis.pojo.Tistit;
import com.mago.tis.service.ITransportService;
import com.mago.tis.utility.ApplicationConstant;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.CollectionUtil;
import com.mago.tis.utility.ExcelReader;
import com.mago.tis.utility.ExceptionMessage;

public class TransportService extends BaseService implements ITransportService {
	private static final Logger logger = Logger
			.getLogger(TransportService.class);
	private ITransportDao transportDao;

	public TransportService() {
		transportDao = new TransportDao();
	}

	/**
	 * Method to save Excel master data
	 */
	public boolean saveExcelData(String excelPath) throws ApplicationException {
		boolean isSaved = false;
		try {

			List<List<String>> excelDataList = (List<List<String>>) ExcelReader
					.readDataFromExcel(excelPath);
			if (CollectionUtil.isEmptyList(excelDataList))
				throw new ApplicationException(
						ApplicationConstant.EXCEL_READ_DATA_EMPTY);
			else {
				int rowCount = transportDao.saveExcel(excelDataList);
				if (rowCount > 0)
					isSaved = true;
			}
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("TransportService --->saveExcelData():"
					+ e.getMessage());
			throw new ApplicationException(
					ApplicationConstant.MASTER_DATA_SAVE_ERROR);
		}
		return isSaved;
	}

	/**
	 * Method that loads the master data from database
	 */
	public List<Tismct> loadMasterData() throws ApplicationException {
		List<Tismct> masterDataList = null;
		try {
			masterDataList = transportDao.getMasterData();
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("TransportService --->loadMasterData():"
					+ e.getMessage());
			throw new ApplicationException(
					ApplicationConstant.MASTER_DATA_FETCH_ERROR);
		}
		return masterDataList;
	}

	public List<Tismct> loadBilledData() throws ApplicationException {
		List<Tismct> billedDataList = null;
		try {
			billedDataList = transportDao.getBilledData();
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("TransportService --->loadBilledData():"
					+ e.getMessage());
			throw new ApplicationException(
					ApplicationConstant.BILLED_DATA_FETCH_ERROR);
		}
		return billedDataList;
	}

	/**
	 * Method to update master data status after billing
	 */
	public boolean updateMasterDataStatusAfterBilling(String challanIds)
			throws ApplicationException {
		boolean isUpdated = false;
		try {
			int rowsUpdated = transportDao.updateMasterData(challanIds);
			if (rowsUpdated > 0)
				isUpdated = true;
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("TransportService --->updateMasterDataStatusAfterBilling():"
					+ e.getMessage());
			throw new ApplicationException(
					ApplicationConstant.CHALLAN_1_DATA_FETCH_ERROR);
		}
		return isUpdated;
	}

	/**
	 * Method to save data of third challan
	 * @param slipNo
	 * @param traderId
	 * @param finalDiff
	 * @param grandTotal
	 * @param grossWeight
	 * @return
	 * @throws ApplicationException
	 */
	public boolean saveChallan(String slipNo, String traderId,
			String finalDiff, String grandTotal, String grossWeight)
			throws ApplicationException {

		ITraderDao traderDao = new TraderDao();
		boolean isSaved = false;
		try {
			Tismbt tismbt = new Tismbt();
			tismbt.setSlipNo(slipNo);
			tismbt.setTraderId(traderId);
			tismbt.setFinalDiff(finalDiff);
			tismbt.setGrandTotal(grandTotal);
			tismbt.setGrossWeight(grossWeight);
			isSaved = transportDao.saveThirdChallan(tismbt);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("TransportService : saveChallan() ----> " + e.getMessage());
			throw new ApplicationException(ExceptionMessage.SAVE_CHALLAN_FAILED);
		}

		return isSaved;
	}

}
